package com.test.testCode.test;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class SortTestA {
    public static void main(String[] args) {

        Random random = new Random();
        IntStream intStream = random.ints(0, 100);
        int[] ints = intStream.limit(60).toArray();
        for (int i : ints) {
            System.out.print(i + ",");
        }
        List<User> list = new ArrayList<>();
        for (int i = 0; i < 59; i++) {
            list.add(new User("user" + i, ints[i]));
        }
        //升序排列
        List<User> sList = list.parallelStream().sorted(Comparator.comparing(User::getAge)).collect(Collectors.toList());
        for (User user : sList) {
            System.out.println(user.toString());
        }
        System.out.println("----------------------------------");
        //降序排列
        sList = list.parallelStream().sorted(Comparator.comparing(User::getAge).reversed()).collect(Collectors.toList());
        for (User user : sList) {
            System.out.println(user.toString());
        }

        int total = sList.size();
        System.out.println("total: " + total);
        int percentE = (int) (Math.floor(total * 0.95));
        int percentD = (int) (Math.floor(total * 0.85));
        int percentDS = (int) (Math.floor(total * 0.75));
        int percentC = (int) (Math.floor(total * 0.60));
        int percentCS = (int) (Math.floor(total * 0.45));
        int percentB = (int) (Math.floor(total * 0.30));
        int percentBS = (int) (Math.floor(total * 0.15));
        int percentA = (int) (Math.floor(total * 0.07));

        List<User> listE = new ArrayList<>();
        List<User> listD = new ArrayList<>();
        List<User> listDS = new ArrayList<>();
        List<User> listC = new ArrayList<>();
        List<User> listCS = new ArrayList<>();
        List<User> listB = new ArrayList<>();
        List<User> listBS = new ArrayList<>();
        List<User> listA = new ArrayList<>();
        List<User> listAS = new ArrayList<>();

        for (int i = sList.size() - 1; i >= 0; i--) {
            User user = sList.get(i);
            if (i >= percentE) {
                user.setGrade("E");
                listE.add(user);
            } else if (i >= percentD) {
                user.setGrade("D");
                listD.add(user);
            } else if (i >= percentDS) {
                user.setGrade("D+");
                listDS.add(user);
            } else if (i >= percentC) {
                user.setGrade("C");
                listC.add(user);
            } else if (i >= percentCS) {
                user.setGrade("C+");
                listCS.add(user);
            } else if (i >= percentB) {
                user.setGrade("B");
                listB.add(user);
            } else if (i >= percentBS) {
                user.setGrade("B+");
                listBS.add(user);
            } else if (i >= percentA) {
                user.setGrade("A");
                listA.add(user);
            } else {
                user.setGrade("A+");
                listAS.add(user);
            }
        }
        contrastList(listE, listD);
        contrastList(listD, listDS);
        contrastList(listDS, listC);
        contrastList(listC, listCS);
        contrastList(listCS, listB);
        contrastList(listB, listBS);
        contrastList(listBS, listA);
        contrastList(listA, listAS);

        System.out.println("dddddddddddddddddd");
    }

    public static void contrastList(List<User> source, List<User> target) {
        //空判断
        int min = target.parallelStream().mapToInt(User::getAge).min().getAsInt();
        Iterator<User> iuser = source.iterator();
        while (iuser.hasNext()) {
            User user = iuser.next();
            if (user.getAge() >= min) {
                user.setGrade(target.get(0).getGrade());
                target.add(user);
                iuser.remove();
            }
        }
    }
}

class User {
    private String name;
    private int age;
    private String grade;

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public User() {
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", grade=" + grade +
                '}';
    }
}
